home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / vector_3d.cp < prev   
Encoding:
Text File  |  1995-03-25  |  8.6 KB  |  157 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    vector_3d.cp
  3. //    Date:                    8/26/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains the methods for a vector_3d
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "point_3d.h"
  11. #include "vector_3d.h"
  12.  
  13. //------------------------------------------------------------------------------
  14. //    global variables
  15. //------------------------------------------------------------------------------
  16. vector_3d    ZERO_VECTOR (R(0.0), R(0.0), R(0.0));                                                                    //    a zero value vector_3d
  17.  
  18. //------------------------------------------------------------------------------
  19. //    constructor
  20. //------------------------------------------------------------------------------
  21. vector_3d::vector_3d (real x, real y, real z, real w) : tuple_3d (x, y, z, w)        //    constructor from three values
  22. {                                                                                                                                                                //    begin
  23. }                                                                                                                                                                //    end
  24.  
  25. //------------------------------------------------------------------------------
  26. //    constructor
  27. //------------------------------------------------------------------------------
  28. vector_3d::vector_3d (const vector_3d &v) : tuple_3d (v)                                                //    copy constructor
  29. {                                                                                                                                                                //    begin
  30. }                                                                                                                                                                //    end
  31.  
  32. //------------------------------------------------------------------------------
  33. //    constructor
  34. //------------------------------------------------------------------------------
  35. vector_3d::vector_3d (const point_3d &p) : tuple_3d (p[X], p[Y], p[Z], R(0.0))    //    constructor from a point_3d
  36. {                                                                                                                                                                //    begin
  37. }                                                                                                                                                                //    end
  38.  
  39. //------------------------------------------------------------------------------
  40. //    constructor
  41. //------------------------------------------------------------------------------
  42. vector_3d::vector_3d (const tuple_3d &t)                                                                                //    constructor from a tuple_3d
  43. {                                                                                                                                                                //    begin
  44.     xyz[X] = t[X]; xyz[Y] = t[Y]; xyz[Z] = t[Z]; xyz[W] = R(0.0);                                    //    copy the values into the tuple_3d
  45. }                                                                                                                                                                //    end
  46.  
  47. //------------------------------------------------------------------------------
  48. //    assignment
  49. //------------------------------------------------------------------------------
  50. vector_3d    &vector_3d::operator = (const vector_3d &v)                                                        //    assignment operator
  51. {                                                                                                                                                                //    begin
  52.     tuple_3d::operator () (v[X], v[Y], v[Z], v[W]);                                                                //    copy the values
  53.     return *this;                                                                                                                                    //    return the reference to this
  54. }                                                                                                                                                                //    end
  55.  
  56. //------------------------------------------------------------------------------
  57. //    assignment
  58. //------------------------------------------------------------------------------
  59. vector_3d    &vector_3d::operator = (const tuple_3d &t)                                                        //    assignment operator
  60. {                                                                                                                                                                //    begin
  61.     tuple_3d::operator () (t[X], t[Y], t[Z], R(0.0));                                                            //    copy the values
  62.     return *this;                                                                                                                                    //    return the reference to this
  63. }                                                                                                                                                                //    end
  64.  
  65. //------------------------------------------------------------------------------
  66. //    assignment by values
  67. //------------------------------------------------------------------------------
  68. void    vector_3d::operator () (real x, real y, real z, real w)                                        //    function call operator
  69. {                                                                                                                                                                //    begin
  70.     tuple_3d::operator () (x, y, z, w);                                                                                        //    assign the coordinates
  71. }                                                                                                                                                                //    end
  72.  
  73. //------------------------------------------------------------------------------
  74. //    scalar multiplication
  75. //------------------------------------------------------------------------------
  76. vector_3d     vector_3d::operator * (real s) const                                                                //    scalar multiplication
  77. {                                                                                                                                                                //    begin
  78.     return vector_3d (xyz[X] * s, xyz[Y] * s, xyz[Z] * s);                                                //    return the multiplied vector_3d
  79. }                                                                                                                                                                //    end
  80.  
  81. //------------------------------------------------------------------------------
  82. //    division by a scalar
  83. //------------------------------------------------------------------------------
  84. vector_3d     vector_3d::operator / (real s) const                                                                //    scalar division
  85. {                                                                                                                                                                //    begin
  86.     return vector_3d (xyz[X] / s, xyz[Y] / s, xyz[Z] / s);                                                //    return the divided vector_3d
  87. }                                                                                                                                                                //    end
  88.  
  89. //------------------------------------------------------------------------------
  90. //    cross product
  91. //------------------------------------------------------------------------------
  92. vector_3d    vector_3d::operator ^ (const vector_3d &v) const                                            //    cross product
  93. {                                                                                                                                                                //    begin
  94.     return vector_3d (    (xyz[Y] * v[Z]) - (xyz[Z] * v[Y]),                                                 //    return a vector_3d, x coordinate
  95.                                     (xyz[Z] * v[X]) - (xyz[X] * v[Z]),                                                        //    y coordinate
  96.                                     (xyz[X] * v[Y]) - (xyz[Y] * v[X]));                                                        //    z coordinate
  97. }                                                                                                                                                                //    end
  98.  
  99. //------------------------------------------------------------------------------
  100. //    addition
  101. //------------------------------------------------------------------------------
  102. vector_3d    vector_3d::operator + (const vector_3d &v) const                                            //    addition operator
  103. {                                                                                                                                                                //    begin
  104.     return vector_3d (    xyz[X] + v[X],                                                                                         //    return a vector_3d, x coordinate
  105.                                     xyz[Y] + v[Y],                                                                                                //    y coordinate
  106.                                     xyz[Z] + v[Z]);                                                                                                //    z coordinate
  107. }                                                                                                                                                                //    end
  108.  
  109. //------------------------------------------------------------------------------
  110. //    self addition
  111. //------------------------------------------------------------------------------
  112. vector_3d    &vector_3d::operator += (const vector_3d &v)                                                    //    self addition operator
  113. {                                                                                                                                                                //    begin
  114.     xyz[X] += v[X];                                                                                                                             //    x coordinate
  115.     xyz[Y] += v[Y];                                                                                                                                //    y coordinate
  116.     xyz[Z] += v[Z];                                                                                                                                //    z coordinate
  117.     return *this;                                                                                                                                    //    return the vector_3d reference
  118. }                                                                                                                                                                //    end
  119.  
  120. //------------------------------------------------------------------------------
  121. //    subtraction
  122. //------------------------------------------------------------------------------
  123. vector_3d    vector_3d::operator - (const vector_3d &v) const                                            //    subtraction operator
  124. {                                                                                                                                                                //    begin
  125.     return vector_3d (    xyz[X] - v[X],                                                                                         //    return a vector_3d, x coordinate
  126.                                     xyz[Y] - v[Y],                                                                                                //    y coordinate
  127.                                     xyz[Z] - v[Z]);                                                                                                //    z coordinate
  128. }                                                                                                                                                                //    end
  129.  
  130. //------------------------------------------------------------------------------
  131. //    negation
  132. //------------------------------------------------------------------------------
  133. vector_3d    vector_3d::operator - (void) const                                                                        //    unary minus operator
  134. {                                                                                                                                                                //    begin
  135.     return vector_3d (-xyz[X], -xyz[Y], -xyz[Z]);                                                                    //    z coordinate
  136. }                                                                                                                                                                //    end
  137.  
  138. //------------------------------------------------------------------------------
  139. //    compute the length
  140. //------------------------------------------------------------------------------
  141. real    vector_3d::Norm (void) const                                                                                            //    compute the length of the vector_3d
  142. {                                                                                                                                                                //    begin
  143.     return SQRT ((*this) | (*this));                                                                                            //    return the square root of the dot product
  144. }                                                                                                                                                                //    end
  145.  
  146. //------------------------------------------------------------------------------
  147. //    reduce to length 1
  148. //------------------------------------------------------------------------------
  149. vector_3d    &vector_3d::Normalize (void)                                                                                    //    reduce the vector_3d to length 1.0
  150. {                                                                                                                                                                //    begin
  151.     real    n = Norm ();                                                                                                                        //    compute the length of the vector_3d
  152.     xyz[X] /= n; xyz[Y] /= n; xyz[Z] /= n;                                                                                //    divide through by the length
  153.     return *this;                                                                                                                                    //    return a referencre to this vector_3d
  154. }                                                                                                                                                                //    end
  155.  
  156. //------------------------------------------------------------------------------
  157.